package com.hqyj.javaGraduation.hotel.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hqyj.javaGraduation.hotel.dao.HotelMaterialsBillDao;
import com.hqyj.javaGraduation.hotel.dao.HotelMaterialsDao;
import com.hqyj.javaGraduation.hotel.pojo.HotelComService;
import com.hqyj.javaGraduation.hotel.pojo.HotelMaterials;
import com.hqyj.javaGraduation.hotel.pojo.HotelMaterialsBill;
import com.hqyj.javaGraduation.hotel.pojo.HotelMenu;
import com.hqyj.javaGraduation.hotel.service.HotelMaterialsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 原材料impl
 */
@Service
public class HotelMaterialsServiceImpl implements HotelMaterialsService {
    @Autowired
    HotelMaterialsDao hotelMaterialsDao;
    @Autowired
    HotelMaterialsBillDao hotelMaterialsBillDao;
    //添加原材料
    @Override
    public HashMap<String, Object> addMaterials(HotelMaterials hotelMaterials) {
        HashMap<String, Object> map = new HashMap<>();
        int num = hotelMaterialsDao.insert(hotelMaterials);
        if (num > 0) {
            map.put("info", "添加成功");
        } else {
            map.put("info", "添加失败");
        }
        return map;
    }
    //修改原材料
    @Override
    public HashMap<String, Object> updateMaterials(HotelMaterials hotelMaterials) {
        HashMap<String, Object> map = new HashMap<>();
        int num = hotelMaterialsDao.updateById(hotelMaterials);
        if (num > 0) {
            map.put("info", "修改成功");
        } else {
            map.put("info", "修改失败");
        }
        return map;
    }
    //删除原材料
    @Override
    public HashMap<String, Object> delMaterials(Integer id) {
        HashMap<String, Object> map = new HashMap<>();
        int num = hotelMaterialsDao.deleteById(id);
        if (num > 0) {
            map.put("info", "删除成功");
        } else {
            map.put("info", "删除失败");
        }
        return map;
    }
    //原材料列表分页
    @Override
    public HashMap<String, Object> MaterialsList(HotelMaterials hotelMaterials) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        //第一步：创建分页对象
        Page<HotelMaterials> hotelMaterialsPage = new Page<HotelMaterials>(hotelMaterials.getPage(), hotelMaterials.getRow());
        //创建查询条件对象
        QueryWrapper<HotelMaterials> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(
                hotelMaterials.getMatName() != null && !hotelMaterials.getMatName().equals(""),
                "mat_name",
                hotelMaterials.getMatName());
        //第二步：查询分页，获取分页数据集
        Page<HotelMaterials> pageList = hotelMaterialsDao.selectPage(hotelMaterialsPage, queryWrapper);
        //获取当前页的集合
        map.put("info", pageList.getRecords());
        //获取总条数
        map.put("total", pageList.getTotal());
        //获取总页数
        map.put("totalPage", pageList.getPages());
        return map;
    }
    @Override
    public HashMap<String, Object> matSupList(HotelComService hotelComService, int matId) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        IPage<HotelComService> page = new Page<>(hotelComService.getPage(), hotelComService.getRow());
        IPage<HotelComService> pageList = hotelMaterialsDao.matSupList(page, matId);
        //获取当前页的集合
        map.put("info", pageList.getRecords());
        //获取总条数
        map.put("total", pageList.getTotal());
        //获取总页数
        map.put("totalPage", pageList.getPages());
        return map;
    }
    //进货
    @Override
    public HashMap<String, Object> updateMatSup(HotelComService hotelComService) {
        HashMap<String, Object> map = new HashMap<>();
        int i = hotelMaterialsDao.selID(hotelComService.getMatId());
        HotelMaterialsBill hotelMaterialsBill = new HotelMaterialsBill();
        hotelMaterialsBill.setBillTime(new Date());
        hotelMaterialsBill.setMatId(hotelComService.getMatId());
        hotelMaterialsBill.setMatName(hotelComService.getMatName());
        hotelMaterialsBill.setMatNumber(hotelComService.getMatNumber());
        hotelMaterialsBill.setMatPrice(hotelComService.getComPrice());
        hotelMaterialsBill.setSupplierName(hotelComService.getSupName());
        hotelMaterialsBill.setBillType("进货");
        int num0 = hotelMaterialsBillDao.insert(hotelMaterialsBill);
        HotelMaterials hotelMaterials = new HotelMaterials();
        hotelMaterials.setMatId(hotelComService.getMatId());
        hotelMaterials.setMatNumber(hotelComService.getMatNumber() + i);
        hotelMaterials.setMatPrice(hotelComService.getMatPrice());
        int num = hotelMaterialsDao.updateById(hotelMaterials);
        if (num > 0 && num0 > 0) {
            map.put("info", "进货成功");
        } else {
            map.put("info", "进货失败");
        }
        return map;
    }
}
